#include <stdio.h> #include <stdbool.h> #include <stdlib.h> typedef struct Node { struct Node* Next; int data; } Node; Node *front,*rear; void initQue(void); void enque(int); bool deque(int *); void printQue(); int main(int argc, const char * argv[]) { int input=0; initQue(); while(true) { printf("Queue with List Program\n"); printf("1. Enque\n"); printf("2. Deque\n"); printf("3. Print Que\n"); printf("4. Exit\n"); printf("Enter your command : "); scanf("%d",&input); getchar(); switch (input) { case 1: printf("Enter a data : "); scanf("%d",&input); getchar(); enque(input); break; case 2: if(deque(&input)) { printf("Data is %d.\n",input); } else { printf("Que is empty.\n"); } break; case 3: printQue(); break; case 4: exit(0); default: break; } } return 0; } void initQue(void) { front = (Node *)malloc(sizeof(Node)); rear = (Node *)malloc(sizeof(Node)); front->Next = rear; rear->Next = rear; } void enque(int data) { Node *tempNode = (Node *)malloc(sizeof(Node)); tempNode->data = data; if(front->Next==rear) { front->Next = tempNode; tempNode->Next = rear; rear->Next = tempNode; } else { rear->Next->Next = tempNode; tempNode->Next = rear; rear->Next = tempNode; } } bool deque(int *data) { if(front->Next==rear) { return false; } Node *tempNode = front->Next; *data = tempNode->data; front->Next = tempNode->Next; free(tempNode); return true; } void printQue() { Node *currentNode = front->Next; if(front->Next==rear) { printf("Que is empty.\n"); return; } printf("Front -> "); while(currentNode!=rear) { printf("%d -> ",currentNode->data); currentNode = currentNode->Next; } printf("Rear\n"); }
You can get every computer informations. Programming, Application, Utilities. Come and see! If you have any question? Contact to me!.
Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts
Thursday, April 9, 2015
[Data Structures] Queue with list in C
Here is Queue data structure with Linked List in C programming language.
Tuesday, April 7, 2015
[Data Structures - C] Circular Queue with Array in C Language
It is "Queue" with Array in C Programming language.
There are some functions get, put, printQue. They are important things.
There are some functions get, put, printQue. They are important things.
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> int *Queue; int max = 0; int front,rear; int command=0; int data; bool isFull = false; void initQue(void); void put(int); bool get(int *data); void printQue(); int main(int argc, const char * argv[]) { initQue(); while(true) { printf("Que Program\n"); printf("1. Put data\n"); printf("2. Get data\n"); printf("3. Print Que\n"); printf("4. Exit Que\n"); printf("Select your command : "); scanf("%d",&command); getchar(); switch (command) { case 1: if (!isFull) { printf("Enter data what you want to put :"); scanf("%d",&command); getchar(); put(command); } else { printf("Que is full. Please do get at least once"); } break; case 2: if(get(&data)) { printf("Getting data is %d\n",data); } else { printf("Que is empty\n"); } break; case 3: printQue(); break; case 4: exit(0); default: break; } } return 0; } void initQue(void) { printf("Please enter volume of Que : "); scanf("%d",&max); getchar(); Queue = (int *)malloc(sizeof(int)*max); front = 0; rear = 0; printf("Que is created with %d volume\n",max); } void put(int data) { if(!isFull) { Queue[rear++] = data; } if(rear>=max) { rear = 0; } if(front==rear) { isFull = true; } } bool get(int *data) { if(front==rear&&!isFull) { return false; } *data = Queue[front++]; if(front>=max) { front=0; } isFull = false; return true; } void printQue() { int tempFront = front; printf("Front -> "); if (front==rear&&!isFull) { printf("Que is Empty."); return; } do { printf("%d -> ",Queue[tempFront++]); if(tempFront>=max) { tempFront = 0; } } while(tempFront!=rear); printf("Rear\n"); }
Monday, April 6, 2015
[Visual Studio] C4996 error solution!
If you use visual studio 2013 or like that 2010,2011,2012...
Have you ever seen c4996 error with scanf?
c4996 error message is it. "error C4996:'scanf': This function or variable may be unsafe. Consider using scan_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Here is solution about that
[It's C4996 error]
Write "#define _CRT_SECURE_NO_WARNINGS" to the top of source code.
[Write lie this!]
Solution 2.
Step 01. Go to Project -> [Your Project Name] Properties...
Step 02. Go to C/C++ -> Preprocessor
Step 03. Click Preprocessor Definitions' Down Arrow button. And click Edit button.
Step 04. Write "_CRT_SECURE_NO_WARNINGS" in the bottom of textarea.
That's it!! It's done! You can compile your source smoothly! Have a nice coding time.
Sunday, April 5, 2015
[Data Structures - C] Stack data structure
It is basic data structure.(Basic data structure is linked list, stack, queue)
If you want to learn about stack. Please watch carefully function pop and push.
If you want to learn about stack. Please watch carefully function pop and push.
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct Node { struct Node *next; char *data; } Node; Node *top; bool push(Node **, char *); bool pop(Node **, char **); void printStack(Node *); int main() { int command = 0; char input = NULL; top = NULL; while (true) { printf("Stack program. Please enter command\n"); printf("1. Push data\n"); printf("2. Pop data\n"); printf("3. Print stack data\n"); printf("4. Exit program\n"); printf("Enter your command : "); scanf("%d", &command); getchar(); switch (command) { case 1: printf("Please enter character what you wnat to push : "); scanf("%c", &input); getchar(); if (push(&top, input)) { printf("Push complete.\n"); } else { printf("Push fail.\n"); } break; case 2: if (pop(&top, &input)) { printf("The data what poped is %c \n", input); } else { printf("There is no element. Please push anything.\n"); } break; case 3: printStack(top); break; case 4: printf("Exit program. Bye Bye\n"); exit(0); default: printf("There is no command like you entered. Please try again. \n"); } } return 0; } bool push(Node **top, char *data) { Node *tempNode = (Node *)malloc(sizeof(Node)); if (!tempNode) return false; tempNode->data = data; tempNode->next = *top; *top = tempNode; return true; } bool pop(Node **top, char **data){ Node *tempNode = *top; if (!tempNode) return false; *data = tempNode->data; *top = tempNode->next; free(tempNode); return true; } void printStack(Node *top) { Node *currentNode = top; while (currentNode!=NULL) { printf("Data - %c \n", currentNode->data); currentNode = currentNode->next; } }
Friday, April 3, 2015
[Data Structure - C] Circular Double Linked List
#include <stdio.h> #include <stdlib.h> typedef struct _Node { char Data; struct _Node *Next; struct _Node *Prev; } Node; Node *head, *tail, *temp; void initList(void); void insertNode(Node *); void deleteNode(Node *); Node *makeNode(char); Node *findNode(char); void printList(void); int main(int argc, const char * argv[]) { int command = 0; char input = 'A'; Node *tempNode; printf("Hello, this is Circular Double Linkded List!\n"); while(1) { printf ("Command List\n"); printf ("1. List Initialization\n"); printf ("2. Insert Node\n"); printf ("3. Delete Node\n"); printf ("4. Print List\n"); printf ("5. Exit List\n"); printf ("Enter your command: "); scanf ("%d",&command); getchar(); switch (command) { case 1: printf("List initializing\n"); initList(); break; case 2: printf("Insert Node\nPlease enter a number what you want to insert : "); scanf("%c",&input); getchar(); tempNode = makeNode(input); insertNode(tempNode); break; case 3: printf("Delete Node\nPlease enter a number what you want to delete : "); scanf("%c",&input); getchar(); tempNode = findNode(input); if(tempNode->Data==input) { deleteNode(tempNode); } else { printf("There is no charactor you insert!\n"); } break; case 4: printf("Node List\n"); printList(); break; case 5: printf("Exit List. Bye bye!"); exit(0); default: printf("There is no command like that!\nPlease retry!"); break; } } return 0; } void initList(void) { head = (Node *)malloc(sizeof(Node)); tail = (Node *)malloc(sizeof(Node)); head->Next = tail; head->Prev = tail; tail->Next = head; tail->Prev = head; } void insertNode(Node *node) { node->Next = tail; node->Prev = tail->Prev; tail->Prev->Next = node; tail->Prev = node; } void deleteNode(Node *node) { node->Prev->Next = node->Next; node->Next->Prev = node->Prev; free(node); } Node *makeNode(char input) { Node *node = (Node *)malloc(sizeof(Node)); node->Data = input; return node; } Node *findNode(char input) { Node *currentNode = head->Next; while(currentNode!=tail&¤tNode->Data != input) { currentNode = currentNode->Next; } return currentNode; } void printList(void) { Node *currentNode = head->Next; while(currentNode != tail) { printf("Node Data is %c \n",currentNode->Data); currentNode = currentNode->Next; } }
Friday, October 3, 2014
[C Data structures and Algorithms] Single Linked List (Circular Linked List)
Hi~! We are here! I will let you know what about Linked List. Exactly Single Circular Linked List with C!
Linked List is so simple and basic data structure. It has front, end node for expression start node and end node. And each nodes have data variable and next pointer variable. Next pointer variable has address next node. End node's next pointer variable point front node.
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _Node { char *Name; struct _Node *Next; } Node; Node *front, *end; // head & tail int currPos, listLength; void initList(void); int insertNode(char *); int afterNode(char *,char *); Node* findNode(char *); Node* findNodeAt(int); void printList(void); int removeNode(char *); void clearList(void); int next(); int prev(); void setFront(); void setEnd(); Node* getNode();
Above source code is ADT for a Single Linked List.
There has to be struct for node. I made _Node struct with Name char pointer variable and Next _Node pinter variable. And define _Node to Node.
Also made front,end currPos, listLength variables. Front and end is for list's start, end. CurrPos and ListLength is for list searcing.
Here is realization source code.
#include "CList.h" void initList(void) { front = (Node*)malloc(sizeof(Node)); end = (Node*)malloc(sizeof(Node)); front->Next = end; front->Name = ""; end->Next = end; end->Name = ""; currPos = 0; listLength = 0; }
initList do initialization list. Do memory alllocate for front, end vars, and set to front's Next point end node, end's Next point front node. This time currPost has 0, listLength is 0.
int insertNode(char *Name) { Node *temp_node = (Node *)malloc(sizeof(Node)); Node *position = front; temp_node->Name = Name; while(position->Next!=end) { position = position->Next; } temp_node->Next = position->Next; position->Next = temp_node; listLength++; return 1; }
Function insertNode has Name argument. If you call insertNode with string. It make temp_node and set Name with string you sent. And temp_node's Next point end. After that last node's Next point temp_node.
And insertion some node between other nodes is same. If you want to insert C node between A,B. C's Next point B after that A's Next point C.
int afterNode(char *Name,char *targetName) { Node *temp_node = (Node *)malloc(sizeof(Node)); Node *position = findNode(targetName); temp_node->Name = Name; if(position!=end) { temp_node->Next = position->Next; position->Next = temp_node; listLength++; return 1; } return -1; } Function afterNode has two arguments Name and targetName. It find the node has targetName and save into position var. And make a temp_node has Name arg. After that, insert temp_node after position.
Node* findNode(char *Name) { Node *start; for(start = front;start!=end;start=start->Next) { if(strcmp(start->Next->Name,Name)==0) { break; } } return start->Next; } Fucntion findNode search whole list and whether node is same Name with Name argument or not. If find node return that.
Node* findNodeAt(int indexAt) { Node *temp_node = front->Next; int index; for(index = 0;index<listLength&&temp_node!=end;index++) { if(index==indexAt) { return temp_node; } temp_node = temp_node->Next; } return NULL; } Function findNodeAt is similar with findNode. But it return node is at some index.
void printList(void) { Node *start; printf("%s","\n\n\nList Items\n"); setFront(); while((start=getNode())!=NULL&&next()) { printf("%s \n",start->Name); } } Function printList print out all list. In here call setFront function.
int removeNode(char *Name) { Node *start = front; Node *target; if((target=findNode(Name))!=start) { while(strcmp(start->Next->Name,Name)!=0) { start = start->Next; } start->Next = target->Next; free(target); listLength--; return 1; } return -1; } Function remove is simple too. find target node and arrange Next pointer. And do memory free.
Arranging Next pointer A node point C, after that clean up B.
void clearList(void) { Node *start = front->Next; Node *next = start->Next; while(next!=end) { free(start); start = next; next = next->Next; } free(front); free(end); initList(); } Function clearList is do memory clean up and call initList function.
Here is next, prev, setFront, setEnd function. These are for changing list position.
int next() { if(currPos < listLength) { currPos++; return currPos; } return -1; } int prev() { if(currPos > 0) { currPos--; return currPos; } return -1; } void setFront() { currPos = 0; } void setEnd() { currPos = listLength-1; } Node* getNode() { return findNodeAt(currPos); }
List is basic data structure. If you want to learn another data structure or algorithms? You have to know List and Pointer. I hope it easy to you. Next posting is Doubly Linked List.
* If you run this program use this main function.
#include "CList.h" int main() { Node* temp; initList(); printf("%s","List Test Program\n"); insertNode("Jake Song"); insertNode("Alex Song"); insertNode("Minhee Kim"); printList(); removeNode("Alex Song"); printList(); clearList(); printList(); return 0; }
Subscribe to:
Posts (Atom)